﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Equipment.Errorcode.ErrorCode
{
    public class PagedResult<T>
    {
        public List<T> Items { get; set; } // 当前页的数据
        public int TotalCount { get; set; } // 总记录数
        public int TotalPages { get; set; } // 总页数
        public int CurrentPage { get; set; } // 当前页码

        public PagedResult(List<T> items, int totalCount, int currentPage, int pageSize)
        {
            Items = items;
            TotalCount = totalCount;
            CurrentPage = currentPage;
            TotalPages = (int)Math.Ceiling((double)totalCount / pageSize); // 计算总页数
        }
    }

    public static class PaginationHelper
    {
        public static PagedResult<T> Paginate<T>(IQueryable<T> source, int pageNumber, int pageSize)
        {
            // 检查页码和每页大小是否合法
            if (pageNumber < 1 || pageSize < 1)
            {
                throw new ArgumentException("页码和每页大小必须大于零。");
            }

            var totalCount = source.Count(); // 获取总记录数
            var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList(); // 获取当前页的数据

            return new PagedResult<T>(items, totalCount, pageNumber, pageSize); // 返回分页结果
        }
    }


}
